package com.lanbu.web.controller.system;

import com.lanbu.common.core.controller.BaseController;
import com.lanbu.common.core.domain.AjaxResult;
import com.lanbu.common.core.domain.entity.SysDept;
import com.lanbu.common.core.domain.entity.SysUser;
import com.lanbu.common.utils.SecurityUtils;
import com.lanbu.system.domain.dto.EvaluationRequest;
import com.lanbu.system.domain.dto.EvaluationResponse;
import com.lanbu.system.domain.mongo.MongoMyUserOutlinksids;
import com.lanbu.system.service.ISysDeptService;
import com.lanbu.system.service.ISysUserService;
import com.lanbu.system.service.IZntEvaluationService;
import com.lanbu.system.service.impl.MongoMyUserOutlinksidsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

import static com.lanbu.web.controller.system.SysUserController.generateUniqueId;

@RestController
@RequestMapping("/api/evaluation")
public class EvaluationController extends BaseController {

    @Autowired
    private IZntEvaluationService zntEvaluationService;

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private MongoMyUserOutlinksidsService mongoMyUserOutlinksidsService;
    @PostMapping("/submit")
    public AjaxResult submitEvaluation(@RequestBody EvaluationRequest request) {
        SysUser sysUser=new SysUser();
        sysUser.setUserName(request.getStudentId());
        sysUser.setNickName(request.getStudentName());
        sysUser.setSex(request.getSex());

        sysUser.setPassword(SecurityUtils.encryptPassword(request.getPassword()));
        //插入用户表py
        if (!userService.checkUserNameUnique(sysUser))
        {
            return error("新增用户'" + sysUser.getUserName() + "'失败，登录账号已存在");
        }
//设置部门id
        SysDept sysDept = deptService.findByDeptName(request.getClassName());
        sysUser.setDeptId(sysDept.getDeptId());
        //设置角色id
        List<Long> roleIdList = Collections.singletonList(request.getRoleId());
        Long[] roleIds = roleIdList.toArray(new Long[0]);
        sysUser.setRoleIds(roleIds);


        int user1 = userService.insertUser(sysUser);
        MongoMyUserOutlinksids mongoMyUserOutlinksids =new MongoMyUserOutlinksids();
        mongoMyUserOutlinksids.setUserId(String.valueOf(sysUser.getUserId()));
        mongoMyUserOutlinksids.setOutlinksid("shareChat-"+generateUniqueId());

        mongoMyUserOutlinksidsService.save(mongoMyUserOutlinksids);
        request.setUserId(sysUser.getUserId());
        zntEvaluationService.saveEvaluation(request);
        return success("新增成功");
    }
    @GetMapping("/findByChartUser")
    public AjaxResult findByChartUser() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        EvaluationResponse byChartUser = zntEvaluationService.findByChartUser(user.getUserId());

        return success(byChartUser);
    }

    @GetMapping("/findByChartUserId")
    public AjaxResult findByChartUserId(Long userId) {

        EvaluationResponse byChartUser = zntEvaluationService.findByChartUser(userId);

        return success(byChartUser);
    }

}